home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / AIAT 1.0.1 / Headers / Common / IACommon.h next >
Encoding:
Text File  |  1997-09-11  |  8.1 KB  |  254 lines  |  [TEXT/CWIE]

  1. // IACommon.h
  2. // Common types, constants, classes and macros used by IA code.
  3.  
  4. #pragma once
  5. #ifndef IACommon_h
  6. #define IACommon_h
  7.  
  8. #pragma import on
  9.  
  10. //    Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  11.  
  12. #ifndef IA_BEGIN_IMPORTS
  13. // the following are used when building shared libraries to define imports & exports
  14. // by default, they're defined with a no-op pragma
  15.  
  16. //#define IA_BEGIN_IMPORTS    mark includes
  17. //#define IA_END_IMPORTS        mark includes
  18. //#define IA_BEGIN_EXPORTS    mark includes
  19. //#define IA_END_EXPORTS        mark includes
  20.  
  21. #define IA_BEGIN_IMPORTS    import on 
  22. #define IA_END_IMPORTS        import reset
  23. #define IA_BEGIN_EXPORTS    export on
  24. #define IA_END_EXPORTS        export off
  25.  
  26. #endif
  27.  
  28. // some inline members generate out-of-line definitions which cause
  29. // conflicts when building a shared library.  the following macros
  30. // allow inlines to be turned off or on, so that library clients see
  31. // only the out-of-line definitions, while the library may use the inline.
  32. #ifdef IA_NO_INLINES
  33. // use the out-of-line definitions from the library
  34. #define IA_INLINE
  35. #define IA_INLINE_DEF();        
  36. #define IA_INLINE_DEF_BODY(BODY);        
  37. #else
  38. // let definitions inline
  39. #define IA_INLINE            inline
  40. #define IA_INLINE_DEF()    {}
  41. #define IA_INLINE_DEF_BODY(BODY)    { BODY; }
  42. #endif
  43.  
  44. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  45.  
  46.  
  47. //#pragma IA_BEGIN_IMPORTS
  48. #include <stdlib.h>
  49. #ifndef IA_NO_EXCEPTIONS
  50. #include <stdexcept>
  51. #endif
  52. #include <string.h>
  53. //#pragma IA_END_IMPORTS
  54.  
  55. #pragma IA_BEGIN_EXPORTS
  56.  
  57. //// ** Primitive types **
  58. /// These names are chosen to be short and to not conflict with others used on the Mac.
  59. typedef unsigned char    byte;
  60. typedef byte            Byte;
  61. typedef long            int32;
  62. typedef unsigned long    uint32;
  63. typedef unsigned short    uint16;
  64. typedef uint16             UInt16;
  65.  
  66. typedef void*    (*IAAllocFptr)(size_t size);  // ptr is a user defined struct
  67. typedef void    (*IADeallocFptr)(void* object); // ptr is a user defined struct
  68.  
  69. //// ** IA globals **
  70. extern uint32            IADiskBlockSize;        // determines default size for most i/o
  71. extern IAAllocFptr        IAAllocationFunc;        // allocation function call back
  72. extern IADeallocFptr    IADeAllocationFunc;        // deallocation function call back
  73.  
  74. //// ** IAMalloc -- all IA memory allocation is done through this **
  75.  
  76. // IAMalloc & IAFree may be used in place of malloc() and free()
  77. void*                    IAMalloc(size_t size);
  78. void                    IAFree(void* object);
  79. // IAMallocSied & IAFreeSized may be used when the size is known at free time.
  80. // They may be faster and/or use less memory.
  81. void*                    IAMallocSized(size_t size);
  82. void                    IAFreeSized(void* object, size_t size);
  83.  
  84. /// handy macros for memory allocation
  85.  
  86. // IAMallocStruct & IAFreeStruct
  87. #define                    IAMallocStruct(TYPE)            (TYPE*)IAMallocSized(sizeof(TYPE))
  88. #define                    IAFreeStruct(MEM,TYPE)            IAFreeSized(MEM, sizeof(TYPE))
  89.  
  90. // IAMallocArray & IAFreeArray
  91. // Caution: this should not be used for classes with virtual member functions (e.g. IAObjects)
  92. // Caution: nor should it be used for classes whose default ctor or dtor do anything
  93. #define                    IAMallocArray(TYPE,SIZE)        (TYPE*)IAMalloc(sizeof(TYPE) * (SIZE))
  94. // we distinguish array deletion to enable substitution of 'delete[]' rather than 'delete'
  95. #define                    IAFreeArray                        IAFree
  96. // variants for sized allocation
  97. #define                    IAMallocArraySized(TYPE,SIZE)    (TYPE*)IAMallocSized(sizeof(TYPE) * (SIZE))
  98. #define                    IAFreeArraySized(MEM,TYPE,SIZE)    IAFreeSized(MEM, sizeof(TYPE) * (SIZE))
  99.  
  100. // in the debug libraries, prints some info about memory usage to the standard output
  101. void                    IAReportMemoryUsage();
  102.  
  103. //// ** IAStruct: a base for IA structs -- allocated by IAMalloc **
  104.  
  105. struct                    IAStruct {
  106. public:
  107.     void*                operator new(size_t size);//        { return IAMalloc(size); }
  108.     void                operator delete(void* object);//    { IAFree(object); }
  109. };
  110.  
  111.  
  112. //// ** IAObject: a base class for Information Access objects **
  113.  
  114. class                    IAObject : public IAStruct {
  115. public:
  116.                         IAObject() {}
  117.     // Ensure objects have a virtual destructor.  This enables IADeleteOnUnwind et. al.
  118.     IA_INLINE virtual    ~IAObject()                                IA_INLINE_DEF()
  119.     void*                operator new(size_t size)                { return IAMallocSized(size); }
  120.     IA_INLINE void        operator delete(void* obj, size_t size) IA_INLINE_DEF_BODY(IAFreeSized(obj, size))
  121. private:
  122.     // Copy constructors cause no end of confusion in C++, so we don't use them.
  123.     // Instead, in any class with a destructor, we:
  124.     // . specify but don't define one (to generate link errors if it is ever called)
  125.     // . make it private so that it can't be specialized.
  126.                         IAObject(IAObject&);
  127. };
  128.  
  129. // enable stack-based deletion of pointers to IAObjects
  130. // this ensures that the destructor is called even when the stack is unwound on exception
  131. // the typical usage is something like:
  132. //   { Foo* foo = FunctionReturningFooStar();
  133. //     IADeleteOnUnwind delFoo(foo);
  134. //     < code using foo> }
  135. class                    IADeleteOnUnwind {
  136. public:
  137.                         IADeleteOnUnwind(IAObject* o) : object(o) {}
  138.                         ~IADeleteOnUnwind();            // delete object;
  139.     IAObject*            object;
  140. private:
  141.     void*                operator new(size_t size);        // stack allocate only
  142. };
  143.  
  144. // permit stack-allocated variable-sized arrays
  145. // the typical usage is something like:
  146. //   { Foo* foos = IAMallocArray(<variable>, <size>);
  147. //     IADeleteArrayOnUnwind delFoos(foos);
  148. //     < code using foos> }
  149. class                    IADeleteArrayOnUnwind {
  150. public:
  151.                         IADeleteArrayOnUnwind(void* a);    // : array(a) {}
  152.                         ~IADeleteArrayOnUnwind();        // { IAFreeArray(array); }
  153.     void*                array;
  154. private:
  155.     void*                operator new(size_t size);        // stack allocate only
  156. };
  157.  
  158. // permit stack-allocated variable-sized arrays of pointers to IAObjects
  159. // the typical usage is something like:
  160. //   { Foo** foos = IAMallocArraySized(Foo*, <size>);
  161. //     < initialize foos >
  162. //     IADeletePointerArrayOnUnwind delFoos(foos, <size>);
  163. //     < code using foos> }
  164. class            IADeletePointerArrayOnUnwind {
  165. public:
  166.                 IADeletePointerArrayOnUnwind(IAObject* a[], uint32 l);
  167.                 ~IADeletePointerArrayOnUnwind();
  168.     IAObject**            array;
  169.     uint32                length;
  170. private:
  171.     void*                operator new(size_t size);        // stack allocate only
  172. };
  173.  
  174.  
  175. //// ** IA Exceptions **
  176.  
  177. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  178. #ifndef IA_NO_EXCEPTIONS
  179.  
  180. class IAException : public exception
  181. {
  182.   public:
  183.                             IAException( const char* message);
  184.     virtual                     ~IAException();
  185.     
  186. //    virtual void            Throw() const;
  187.     virtual const char*     What() const;
  188.     virtual const char*        GetLocation() const;
  189.     virtual int32             GetCode() const;
  190.     
  191.             void             SetLocation (const char* location);
  192.             void             SetCode (int32 code);
  193.     
  194.   private:
  195.   
  196.       virtual const char*     what() const;
  197.       
  198.             char            fMessage[250];
  199.             char            fLocation[50];
  200.             int32            fCode;
  201. };
  202.  
  203.  
  204. #else
  205. // until compilers have better exception support, just throw integers
  206. typedef const int32        IAException;
  207. #endif
  208.  
  209. typedef const int32     IAExceptionCode;
  210.  
  211. // don't inline calls to throw() -- makes code smaller & faster!
  212. void IAThrowException(IAException exception);    // { throw(exception); }
  213.  
  214. //
  215.  
  216. void _IAAssertionFunc(const char*, int, const char*, const char*, IAExceptionCode code);
  217. #define IAAssertion(expression, message, code) \
  218.     ( (expression) ? (void) 0 : (_IAAssertionFunc(__FILE__, __LINE__, #expression, message, code)) )
  219. //
  220.  
  221. // define IAThrow to shadow definitions in this file
  222. #ifndef IAThrow
  223.  
  224.  
  225. // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
  226. #ifndef IA_NO_EXCEPTIONS
  227. // exceptions are ANSI by default
  228. #define IAThrow(EXCEPTION)    IAThrowException(EXCEPTION) // throw(exception)
  229. #define IATry                try
  230. #define IACatch(BINDING)    catch (BINDING)
  231.  
  232. #else
  233.  
  234. // definitions for compilers that don't support exceptions
  235. #define IAThrow(EXCEPTION)    IAThrowException(EXCEPTION) // exit(EXCEPTION)
  236. #define IATry                if (true)
  237. #define IACatch(BINDING)    for (BINDING = 0; false; )
  238.  
  239. #endif
  240. #endif
  241.  
  242. IAExceptionCode                IAAssertionFailure = 'VTWN';
  243.  
  244. // handy macros which conditionally throw exceptions
  245. #define IAAssert(VALUE)                    if (!(VALUE))    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAAssert", IAAssertionFailure))
  246. #define IAThrowIf(VALUE, EXCEPTION)        if (  VALUE )    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIf", EXCEPTION))
  247. #define IAThrowIfNot(VALUE, EXCEPTION)    if (!(VALUE))    (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIfNot", EXCEPTION))
  248.  
  249. #pragma IA_END_EXPORTS
  250.  
  251. #pragma import reset
  252.  
  253. #endif
  254.